package com.sinodata.bsm.cicp.util.snmp;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Vector;

import net.percederberg.mibble.Mib;
import net.percederberg.mibble.MibLoader;
import net.percederberg.mibble.MibLoaderException;
import net.percederberg.mibble.MibValueSymbol;

import org.snmp4j.PDU;
import org.snmp4j.smi.Null;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-8-26 上午10:52:53          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class SnmpUtil {
    private static final MibLoader mibLoader = new MibLoader();

    static {
        try {
            mibLoader.load("RFC1213-MIB");
            mibLoader.load("HOST-RESOURCES-MIB");
            mibLoader.load("BRIDGE-MIB");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MibLoaderException e) {
            e.printStackTrace();
        }
    }

    public static MibValueSymbol getMibSymbolByOid(String oid) throws Exception {
        if (oid.startsWith("[.]"))
            oid = oid.replaceFirst("[.]", "");
        Mib[] arrayOfMib = mibLoader.getAllMibs();
        MibValueSymbol localMibValueSymbol = null;
        for (int i = 0; i < arrayOfMib.length; i++) {
            localMibValueSymbol = arrayOfMib[i].getSymbolByValue(oid);
            if (localMibValueSymbol != null)
                break;
        }
        if (localMibValueSymbol == null) {
            throw new Exception();
        }
        return localMibValueSymbol;
    }

    public static Vector<MibValueSymbol> getFieldsInMibNode(OMMappingInfo paramClass, MibValueSymbol paramMibValueSymbol) {
        Field[] arrayOfField = paramClass.getClass().getDeclaredFields();
        Vector<MibValueSymbol> localVector = new Vector<MibValueSymbol>();
        for (int i = 0; i < arrayOfField.length; i++) {
            String str = arrayOfField[i].getName();
            MibValueSymbol[] arrayOfMibValueSymbol = paramMibValueSymbol.getChildren();
            for (int j = 0; j < arrayOfMibValueSymbol.length; j++)
                if (arrayOfMibValueSymbol[j].getName().equals(str)) {
                    localVector.add(arrayOfMibValueSymbol[j]);
                    break;
                }
        }
        return localVector;
    }

    public static void setFieldValue(OMMappingInfo mibSystem, String paramString, Variable paramVariable) {
        try {
            String str1 = "set" + paramString.replaceFirst(paramString.substring(0, 1), paramString.substring(0, 1).toUpperCase());
            Field localField = mibSystem.getClass().getDeclaredField(paramString);
            if (localField == null) {
                return;
            }
            Method localMethod = mibSystem.getClass().getMethod(str1, new Class[] { localField.getType() });
            String str2 = localField.getType().getName();
            if (str2.equalsIgnoreCase("int")) {
                int i = paramVariable.toInt();
                if ((paramVariable instanceof Null))
                    i = 0;
                localMethod.invoke(mibSystem, new Object[] { new Integer(i) });
            } else if (str2.equalsIgnoreCase("long")) {
                long l = paramVariable.toLong();
                if ((paramVariable instanceof Null))
                    l = 0L;
                localMethod.invoke(mibSystem, new Object[] { new Long(l) });
            } else if (str2.equalsIgnoreCase("java.lang.String")) {
                localMethod.invoke(mibSystem, new Object[] { paramVariable.toString() });
            }
        } catch (Exception localException) {
        }
    }

    public static PDU createGetPdu(String oid) {
        PDU pdu = new PDU();
        pdu.setType(PDU.GET);
        pdu.add(new VariableBinding(new OID(oid)));
        return pdu;
    }
}
